package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/13 11:07 下午
 */
public class 剑指_Offer_28_对称的二叉树 {

    /**
     * DFS
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return subTreeIsSymmetric(root.left,root.right);
    }

    private boolean subTreeIsSymmetric(TreeNode left, TreeNode right) {
        int tagLeft = (left == null) ? 0 : 1;
        int tagRight = (right == null) ? 0 : 1;
        if ((tagLeft^tagRight) == 1) {
            return false;
        } else if (tagLeft == 0) {
            return true;
        } else {
            return left.val == right.val && subTreeIsSymmetric(left.left,right.right) && subTreeIsSymmetric(left.right,right.left);
        }
    }
}
